<cfcomponent output="false">
	<cfset $ = application.helpers />
	<cfset $view = application.v />
	<cfset $service = application.s />
	
	<!--- --->
	
	<cffunction name="renderInterface" access="public" output="true" returntype="string">
		<cfargument name="fundID" required="true" />

		<cfset var local = {} />
		
		<cfset local.query = queryNew('id,name,value') />
		<cfset queryAddRow(local.query) />
		<cfset querySetCell(local.query,'id','') />
		<cfset querySetCell(local.query,'name','- New Mailing -') />
		<cfset querySetCell(local.query,'value','') />
		<cfset queryAddRow(local.query) />
		<cfset querySetCell(local.query,'id',123456678890) />
		<cfset querySetCell(local.query,'name','Initial Mailing') />
		<cfset querySetCell(local.query,'value',123456678890) />
		
		<cfsavecontent variable="local.result">
			<div>
				#renderSelect(arguments.fundID)#
				<span class="instructions" style="padding-left:4px;cursor:pointer;"><button class="small" onclick="$('[name=mailing_#arguments.fundID#]').val('');Mailing.changeMailing('#arguments.fundID#');">add</button></span>
			</div>
			
			<div>
				<span class="subInstruction">Mailing Name:</span>
				<input title="Mailing Name" validate="required" class="small" name="name_#arguments.fundID#" value="Initial Mailing" style="width:102px;margin-left:5px;">
			</div>
			
			<div>
				<span class="subInstruction">From:&nbsp;&nbsp;<b>mailer@fundr.us</b></span>
			</div>
			
			<div>
				<span class="subInstruction">Subject:</span>
				<input title="Subject" validate="required" class="small" name="subject_#arguments.fundID#"  style="width:127px;margin-left:5px;">
			</div>
			
			<textarea validate="required" title="Mailing Body" name="body_#arguments.fundID#" style="width:169px;height:65px;overflow-y:auto;" onfocus="NameSwap.stop('#arguments.fundID#')" onblur="NameSwap.start('#arguments.fundID#');">
				loading...
			</textarea>
			
			<input type="hidden" name="from_#arguments.fundID#" />
			<input type="hidden" name="mailingID_#arguments.fundID#" />
			
			<div style="text-align:right;">
				<button class="small buttonPreview" style="margin-top:0px;" onclick="">preview</button>
				<button class="small buttonSave" style="margin-top:0px;" onclick="Mailing.saveMailing('#arguments.fundID#',this);">save</button>
				<button class="small buttonDelete" style="margin-top:0px;" onclick="Mailing.deleteMailing('#arguments.fundID#',this)">delete</button>
				<button class="small disabled buttonSend" style="margin-top:0px;">send</button>
			</div>
			
		</cfsavecontent>
		
					
		<cfreturn $.render.clean(local.result) />
		
	</cffunction>
	
	<!--- --->
		
	<cffunction name="renderSelect" access="public" output="true" returntype="string">
		<cfargument name="fundID" required="true" />
		<cfargument name="mailingID" required="false" default="" />

		<cfset var local = {} />
		
		<cfset sleep(300) />
		
		<cfset local.fund = application.s.fundService.getIDByFundID(arguments.fundID) />
		
		<cfset local.mailings = application.s.mailingService.getMailings(fundID=local.fund.id) />
		
		<cfset local.mailingID = arguments.mailingID />
		<cfif local.mailingID eq ''>
			<cfset local.mailingID = local.mailings.id />
		</cfif>
		
		<cfsavecontent variable="local.result">
			#$.render.select(
				name="mailing_#arguments.fundID#",
				label="Mailings",
				blank=true,
				query=local.mailings,
				option="- New Mailing -",
				onchange="Mailing.changeMailing('#arguments.fundID#');",
				value=local.mailingID
			)#

			<!--- put the mailings in js memory --->
			<script type="text/javascript">
				setTimeout(function(){
					Compose.funds['#arguments.fundID#'] = {};
					Compose.funds['#arguments.fundID#'].mailings = {};
					<cfloop query="local.mailings">
						Compose.funds['#arguments.fundID#'].mailings['#local.mailings.id#'] = {};
						Compose.funds['#arguments.fundID#'].mailings['#local.mailings.id#'].id = '#local.mailings.id#';
						Compose.funds['#arguments.fundID#'].mailings['#local.mailings.id#'].name = '#local.mailings.name#';
						Compose.funds['#arguments.fundID#'].mailings['#local.mailings.id#'].from = '#local.mailings.email_from#';
						Compose.funds['#arguments.fundID#'].mailings['#local.mailings.id#'].subject = '#local.mailings.subject#';
						Compose.funds['#arguments.fundID#'].mailings['#local.mailings.id#'].body = '#replaceNoCase(local.mailings.body,Chr(10),'\n','ALL')#';
					</cfloop>
					Mailing.changeMailing('#arguments.fundID#');
				},200);
			</script>
		</cfsavecontent>
		
					
		<cfreturn $.render.clean(local.result) />
		
	</cffunction>
	
	<!--- --->


</cfcomponent>